from sqlalchemy import Column, Integer, String, CHAR
from sqlalchemy.dialects.mysql import LONGTEXT
from datetime import datetime
from models.base import db, Base


class taobao_orders(Base):
    __tablename__ = 'taobao_orders'
    id = Column(Integer, primary_key=True, autoincrement=True)
    order_number = Column(String(30), nullable=False, unique=True)
    create_time = Column('create_time', Integer)
    remain = Column(Integer, nullable=False, default=0)
    shop = Column(String(30), default='')
    order_detail = Column(String(180), default='')
    Logistics_log = Column(String(100), default='')

    def __init__(self):
        self.create_time = int(datetime.now().timestamp())

    # @classmethod
    # def is_useful(cls, order_number, ):
    #     order = cls.query.filter_by(order_number=order_number).first()
    #     if order and order.remain:
    #
    #     else:
    #         return True


    def get_status(self, order_number):
        order_status = db.query(self).filter_by(order_number=order_number).first()
        if order_status:
            return order_status.status
        else:
            return False

    @property
    def create_datetime(self):
        if self.create_time:
            return datetime.fromtimestamp(self.create_time)
        else:
            return None

    def search_order(self, order_number):
        order_info = db.query(self).filter_by(order_number=order_number).first()
        return order_info if order_info else False

    def update_status(self, order_number):
        # user_result = session.query(User).filter_by(id='1').first()
        sql = "update orders set status = '1' where order_number = %s" % order_number
        db.execute(sql)
